package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateRoom;
import com.ruoyi.system.domain.module.estate.EstateRoomBangUser;
import com.ruoyi.system.service.module.estate.IEstateRoomBangUserService;
import com.ruoyi.system.service.module.estate.IEstateRoomService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.getUser;

/**
 * 小区房屋绑定微信用户Controller
 *
 * @author ruoyi
 * @date 2023-08-19
 */
@Validated
@Api(value = "小区房屋绑定微信用户控制器", tags = {"小区房屋绑定微信用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estateUserBang")
public class EstateRoomBangUserController extends BaseController {

    private final IEstateRoomBangUserService estateRoomBangUserService;
    private final IEstateRoomService estateRoomService;
	private final IEstateService estateService;

    /**
     * 查询小区房屋绑定微信用户列表
     */
    @ApiOperation("查询小区房屋绑定微信用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateRoomBangUser> list(@Validated EstateRoomBangUser estateRoomBangUser) {
        QueryWrapper<EstateRoomBangUser> qw = new QueryWrapper<>();
        if (estateRoomBangUser.getBangWxUserId()!=null){
            qw.eq("bang_wx_user_id",estateRoomBangUser.getBangWxUserId());
        }
        TableDataInfo<EstateRoomBangUser> td = PageUtils.buildDataInfo(estateRoomBangUserService.page(PageUtils.buildPage(),qw));
        List<EstateRoomBangUser> list = td.getRows();
        return td;
    }


	/**
	 * 查询小区房屋绑定微信用户数量
	 */
	@ApiOperation("查询小区房屋绑定微信用户数量")
	@PreAuthorize("@ss.hasPermi('module:estate:list')")
	@GetMapping("/num")
	public Integer bangNum() {
		SysUser sysUser = getUser();
		Integer num = 0;
		if(sysUser.getUserType()==2){
			if (sysUser.getEstateId()==null){
				QueryWrapper<Estate> qw1 = new QueryWrapper<>();
				if (sysUser.getPropertyId()!=null){
					qw1.eq("property_id",sysUser.getPropertyId());
				}
				List<Estate> estates = estateService.list(qw1);
				for (Estate estate : estates){
					QueryWrapper<EstateRoomBangUser> qw = new QueryWrapper<>();
					if (estate.getId()!=null){
						qw.eq("estate_id",estate.getId());
					}
					TableDataInfo<EstateRoomBangUser> td = PageUtils.buildDataInfo(estateRoomBangUserService.page(PageUtils.buildPage(),qw));
					List<EstateRoomBangUser> list = td.getRows();
					num += list.size();
				}
			}else {
				QueryWrapper<EstateRoomBangUser> qw = new QueryWrapper<>();
				if (sysUser.getEstateId()!=null){
					qw.eq("estate_id",sysUser.getEstateId());
				}
				TableDataInfo<EstateRoomBangUser> td = PageUtils.buildDataInfo(estateRoomBangUserService.page(PageUtils.buildPage(),qw));
				List<EstateRoomBangUser> list = td.getRows();
				num = list.size();
			}
		}else if (sysUser.getUserType()==0){
			num = 21868;
		}else {
			QueryWrapper<EstateRoomBangUser> qw = new QueryWrapper<>();
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
			TableDataInfo<EstateRoomBangUser> td = PageUtils.buildDataInfo(estateRoomBangUserService.page(PageUtils.buildPage(),qw));
			List<EstateRoomBangUser> list = td.getRows();
			num = list.size();
		}

		return num;
	}

    /**
     * 导出小区房屋绑定微信用户列表
     */
    @ApiOperation("导出小区房屋绑定微信用户列表")
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "小区房屋绑定微信用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateRoomBangUser> export(@Validated EstateRoomBangUser estateRoomBangUser) {
        List<EstateRoomBangUser> list = estateRoomBangUserService.list();
        ExcelUtil<EstateRoomBangUser> util = new ExcelUtil<EstateRoomBangUser>(EstateRoomBangUser.class);
        return util.exportExcel(list, "小区房屋绑定微信用户");
    }

    /**
     * 获取小区房屋绑定微信用户详细信息
     */
    @ApiOperation("获取小区房屋绑定微信用户详细信息")
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateRoomBangUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateRoomBangUser estateRoomBangUser = estateRoomBangUserService.getById(id);
        return AjaxResult.success(estateRoomBangUser);
    }

    /**
     * 新增小区房屋绑定微信用户
     */
    @ApiOperation("新增小区房屋绑定微信用户")
    @PreAuthorize("@ss.hasPermi('module:estateBangUser:add')")
    @Log(title = "小区房屋绑定微信用户", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateRoomBangUser estateRoomBangUser) {
        QueryWrapper<EstateRoom> qw = new QueryWrapper<>();
        if(estateRoomBangUser.getEstateId()!=null){
            qw.eq("estate_id",estateRoomBangUser.getEstateId());
        }
        List<EstateRoom> list = estateRoomService.list(qw);
        for (EstateRoom list1:list){
            EstateRoomBangUser estateRoomBangUser1 = new EstateRoomBangUser();
            estateRoomBangUser1.setEstateId(estateRoomBangUser.getEstateId());
            estateRoomBangUser1.setBangWxUserId(estateRoomBangUser.getBangWxUserId());
            estateRoomBangUser1.setRoomId(list1.getId());
            boolean b = estateRoomBangUserService.save(estateRoomBangUser1);
            if (b==false){
                return toAjax(false);
            }
        }
        return toAjax(true);
    }

    /**
     * 修改小区房屋绑定微信用户
     */
    @ApiOperation("修改小区房屋绑定微信用户")
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "小区房屋绑定微信用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateRoomBangUser estateRoomBangUser) {
        return toAjax(estateRoomBangUserService.updateById(estateRoomBangUser) ? 1 : 0);
    }

    /**
     * 删除小区房屋绑定微信用户
     */
    @ApiOperation("删除小区房屋绑定微信用户")
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "小区房屋绑定微信用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateRoomBangUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
